package com.example.truyencotich;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.example.truyencotich.support.CurlPage;
import com.example.truyencotich.support.CurlView;
import com.example.truyencotich.support.Global;
import com.example.truyencotich.support.SoundManager;
import com.example.truyencotich.R;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class CayTreActivity extends Activity implements OnClickListener,
		OnCheckedChangeListener {
	private CurlView mCurlView;
	private LinearLayout llayoutADS;
	private AdView adView;
	private Button btnHome;
	ToggleButton btnSound;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.story_activity);

		int index = 0;
		if (getLastNonConfigurationInstance() != null) {
			index = (Integer) getLastNonConfigurationInstance();
		}
		mCurlView = (CurlView) findViewById(R.id.curl);
		mCurlView.setPageProvider(new PageProvider());
		mCurlView.setSizeChangedObserver(new SizeChangedObserver());
		mCurlView.setCurrentIndex(index);
		mCurlView.setBackgroundColor(0xFF202830);

		Global.sm.playSound(Global.arrPathSoundTre[0]);

		// Quảng cáo
		adView = new AdView(this, AdSize.BANNER, Global.id_ads);
		llayoutADS = (LinearLayout) findViewById(R.id.llayoutADS);
		llayoutADS.addView(adView);
		// Create an ad request. Check logcat output for the hashed device ID to
		// get test ads on a physical device.
		AdRequest adRequest = new AdRequest();
		// adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
		adRequest.addTestDevice("4E1F099A94A7FF2966392AEDC7C78FEB");
		// Start loading the ad in the background.
		adView.loadAd(new AdRequest());

		btnHome = (Button) findViewById(R.id.btnHome);
		btnHome.setOnClickListener(this);
		btnSound = (ToggleButton) findViewById(R.id.btnSound);
		btnSound.setOnCheckedChangeListener(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		mCurlView.onPause();
		Global.sm.pauseSound();
	}

	@Override
	public void onResume() {
		super.onResume();
		mCurlView.onResume();
		Global.sm.resumeSound();
	}

	@Override
	protected void onDestroy() {
		Global.sm.stopSound();
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btnHome)
			finish();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			Global.isMute = false;
			if (Global.indexCurrSong <= Global.arrPathSoundTre.length - 1)
				Global.sm
						.playSound(Global.arrPathSoundTre[Global.indexCurrSong]);
		} else {
			Global.isMute = true;
			Global.sm.stopSound();
		}
	}

	@Override
	public Object onRetainNonConfigurationInstance() {
		return mCurlView.getCurrentIndex();
	}

	/**
	 * Bitmap provider.
	 */
	private class PageProvider implements CurlView.PageProvider {
		@Override
		public int getPageCount() {
			return Global.mBitmapIds_Tre.length;
		}

		private Bitmap loadBitmap(int width, int height, int index) {
			Bitmap b = Bitmap.createBitmap(width, height,
					Bitmap.Config.ARGB_8888);
			b.eraseColor(0xFFFFFFFF);
			Canvas c = new Canvas(b);
			Drawable d = getResources().getDrawable(
					Global.mBitmapIds_Tre[index]);

			int margin = 1;
			int border = 1;
			Rect r = new Rect(margin, margin, width - margin, height - margin);

			int imageWidth = r.width() - (border * 2);
			int imageHeight = imageWidth * d.getIntrinsicHeight()
					/ d.getIntrinsicWidth();
			if (imageHeight > r.height() - (border * 2)) {
				imageHeight = r.height() - (border * 2);
				imageWidth = imageHeight * d.getIntrinsicWidth()
						/ d.getIntrinsicHeight();
			}

			r.left += ((r.width() - imageWidth) / 2) - border;
			r.right = r.left + imageWidth + border + border;
			r.top += ((r.height() - imageHeight) / 2) - border;
			r.bottom = r.top + imageHeight + border + border;

			Paint p = new Paint();
			p.setColor(0xFFC0C0C0);
			c.drawRect(r, p);
			r.left += border;
			r.right -= border;
			r.top += border;
			r.bottom -= border;

			d.setBounds(r);
			d.draw(c);

			return b;
		}

		@Override
		public void updatePage(CurlPage page, int width, int height, int index) {
			Bitmap front = loadBitmap(width, height, index);
			page.setTexture(front, CurlPage.SIDE_BOTH);
			page.setColor(Color.argb(110, 255, 255, 255), CurlPage.SIDE_BACK);
		}
	}

	/**
	 * CurlView size changed observer.
	 */
	private class SizeChangedObserver implements CurlView.SizeChangedObserver {
		@Override
		public void onSizeChanged(int w, int h) {
			mCurlView.setViewMode(CurlView.SHOW_ONE_PAGE);
			mCurlView.setMargins(.1f, 0f, .05f, 0f);
		}
	}
}
